package com.ynunicom.dc.dingdingcontractapp.entity;

import com.alibaba.fastjson.JSONObject;
import com.ynunicom.dc.dingdingcontractapp.dto.ResponseDto;
import com.ynunicom.dc.dingdingcontractapp.entity.requestbodyentity.TransBody;
import com.ynunicom.dc.dingdingcontractapp.entity.entityinterface.StandReturner;
import com.ynunicom.dc.dingdingcontractapp.utils.SpringHelper;
import lombok.NoArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;
import java.util.Set;

/**
 * @author: jinye.Bai
 * @date: 2020/5/27 14:38
 */

@Service
@NoArgsConstructor
public class StandReturnerImpl implements StandReturner {

    private static SpringHelper springHelper = new SpringHelper();

    @SneakyThrows
    @Override
    public ResponseDto<JSONObject> doReturn(TransBody transBody, Class<?> clazz, Method method) {
        Set<String> set = null;
        if((set=springHelper.getBeansOfType(clazz).keySet()).isEmpty()){
            return ResponseDto.failed("service调用出错");
        }
        JSONObject jsonObject = (JSONObject) method.invoke(springHelper.getBean(String.valueOf(set.toArray()[0])),transBody);
        return Integer.parseInt(jsonObject.getString("errcode"))!=0? ResponseDto.failed("请求失败",jsonObject): ResponseDto.success(jsonObject);
    }
}
